package com.mikepenz.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.mikepenz.bean.OrderBean;
import com.mikepenz.bean.RequestBean;
import com.mikepenz.interactor.OrderInteractor;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.MethodCode;
import com.mikepenz.utils.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lilongfei on 15/10/4.
 */
public class OrderInteractorImp extends NetWorkImp implements OrderInteractor {

    RequestListener listener;
    public OrderInteractorImp(Context c) {
        this(c,(RequestListener)c);
    }
    public OrderInteractorImp(Context c, RequestListener listener){
        super(c);
        this.listener = listener;
    }

    @Override
    public void getOrderList(String userId, String parkId,String type, int currentPage) {
        RequestBean bean = new RequestBean();
        Map<String,Object> params = new HashMap<>();
        params.put("parkId",parkId);
        params.put("orderType",type);
        bean.setRequestParams(params);
        bean.setRequestContentCurrentPage(currentPage);
        bean.setRequestCode(MethodType.MANAGER_ORDER_LIST);
        addFastJsonQueue(Request.Method.POST, MethodType.MANAGER_ORDER_LIST, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }

    @Override
    public void getOrder(String userId, String orderId) {
        RequestBean bean = new RequestBean();
        Map<String,Object> params = new HashMap<>();
        params.put("orderCode",orderId);
        bean.setRequestParams(params);
        bean.setUserSession(userId);
        bean.setRequestCode(MethodType.ORDER_DETAIL);
        addFastJsonQueue(Request.Method.POST, MethodType.ORDER_DETAIL, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }


    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {
        List<OrderBean> orders = new ArrayList<>();

        if (result == null){
            onRequestExcption(null);
            return;
        }

        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        int statue = response.getInteger(MethodCode.responseStatue);

        if (statue == (MethodCode.NetState.SUCCESS.getValue())) {

            if (tag.toString().equals(MethodType.MANAGER_ORDER_LIST)) {
                JSONObject obj = response.getJSONObject(MethodCode.OBJECT);
                JSONArray order = obj.getJSONArray("orders");
                for (int i = 0; i < order.size(); i++) {
                    JSONObject p = order.getJSONObject(i);
                    if (p != null) {
                        OrderBean bean = JSONObject.toJavaObject(p, OrderBean.class);
                        orders.add(bean);
                    }
                }
                listener.Success(tag+"", orders);
            }
            else if (tag.toString().equals(MethodType.ORDER_DETAIL)){
                JSONObject obj = response.getJSONObject(MethodCode.OBJECT);
                JSONObject car = obj.getJSONObject("order");
                OrderBean bean = JSON.parseObject(car.toJSONString(),OrderBean.class);
                listener.Success(tag+"", bean);
            }
        }
        else
            listener.Error(tag + "", "获取订单失败");
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        listener.Exception(tag,error);
    }
}
